package com.cn.api.file;

import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

@Service
public class FileService {

	public void mkdirs(String fold) {
		File file = new File(fold);
		if (file.exists()) {
			return;
		}

		file.mkdirs();
	}

	/**
	 * 取文件
	 */
	public String getMime(File file) {
		String mime = null;
		try {
			mime = file.toURI().toURL().openConnection().getContentType();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mime;
	}

	/**
	 * 改变图像尺寸
	 * 
	 * @throws IOException
	 */
	public File resizeImage(File imageFile, String fileName, int width) throws IOException {

		// 文件已存在则 直接返回
		File resizedImage = new File(imageFile.getParent(),width + "_" + fileName);
		if (resizedImage.exists()) {
			return resizedImage;
		} else {
			File path = resizedImage.getParentFile();
			if (!path.exists()) {
				path.mkdirs();
			}
		}

		Image src = Toolkit.getDefaultToolkit().getImage(imageFile.getPath());

		MediaTracker mediaTracker = new MediaTracker(new Container());

		mediaTracker.addImage(src, 0);
		try {
			mediaTracker.waitForID(0);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		BufferedImage image = new BufferedImage(src.getWidth(null), src.getHeight(null), BufferedImage.TYPE_INT_RGB);
		image.getGraphics().drawImage(src, 0, 0, null);

		int imageType = BufferedImage.TYPE_INT_RGB;
		if (image.isAlphaPremultiplied()) {
			imageType = Transparency.TRANSLUCENT;
		}

		// 生成新尺寸图片
		if (image.getWidth() > width) {
			BufferedImage temp = new BufferedImage(width, width * image.getHeight() / image.getWidth(), imageType);
			temp.getGraphics().drawImage(image.getScaledInstance(width, -1, Image.SCALE_SMOOTH), 0, 0, null);
			image = temp;

			ImageIO.write(image, "JPEG", resizedImage);
		} else {
			return imageFile;
		}

		return resizedImage;
	}

	/**
	 * 从临时文件夹移出文件
	 * 
	 * @param photoName
	 */
	public void moveFromTemp(String photoName) {
		mkdirs(/* Configs.UPLOAD_FOLD */"");

		File temp = new File(/* Configs.TEMP_FOLD */"" + photoName);
		File target = new File(/* Configs.UPLOAD_FOLD */"" + photoName);

		if (!temp.exists()) {

		}

		temp.renameTo(target);
	}

	/**
	 * 把文件移动到临时文件夹
	 * 
	 * @param photoName
	 */
	public void moveToTemp(String photoName) {
		mkdirs(/* Configs.TEMP_FOLD */"");

		File temp = new File(/* Configs.TEMP_FOLD */ "" + photoName);
		File target = new File(/* Configs.UPLOAD_FOLD */ "" + photoName);

		if (!target.exists()) {
			return;
		}

		target.renameTo(temp);
	}

	/**
	 * 删除文件
	 * 
	 * @param photoName
	 */
	public void deletePhoto(String photoName) {
		File file = new File(photoName);
		if (!file.exists()) {
			return;
		}
		if (!file.delete()) {

			System.gc();
			file.delete();
		}
	}

	public File getArFile(File rfile, String fileName) throws IOException {
		// 文件已存在则 直接返回
		File wfile = new File(/* Configs.UPLOAD_FOLD */"" + "_" + fileName);
		if (wfile.exists()) {
			return wfile;
		} else {
			File path = wfile.getParentFile();
			if (!path.exists()) {
				path.mkdirs();
			}
		}

		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(rfile)));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(wfile)));
		String temp = null;
		temp = br.readLine();
		while (temp != null) {
			// 写文件
			if (temp.indexOf("JGDM") > 0) {
				bw.write(temp + "/r/n"); // 只适用Windows系统

			}
			// 继续读文件
			temp = br.readLine();
		}

		bw.close();
		br.close();
		return wfile;
	}

}
